package com.ys.servers.points.dao.impl;

import com.ys.servers.common.dao.BasicNoSqlDao;
import com.ys.servers.common.data.UserType;
import com.ys.servers.points.dao.UserPointsLogDao;
import com.ys.servers.points.data.UserPointsLog;
import io.vertx.core.Future;

import java.util.List;

/**
 * Created by taotao on 2017/5/10.
 */
public class UserPointsLogDaoImpl extends BasicNoSqlDao<UserPointsLog> implements UserPointsLogDao {

    @Override
    public Future<UserPointsLog> addUserPointsLog(UserPointsLog userPointsLog) {

        return this.addData(userPointsLog);
    }

    @Override
    public Future<Boolean> addUserPointsLogToRedisList(long days, UserPointsLog userPointsLog) {
        return dataReader.addToRedisSortedList(genRecentDayListKeys(days, userPointsLog), userPointsLog);
    }

    @Override
    public Future<Long> refreshRecentLogs(long days, String userId, UserType userType) {
        long time = System.currentTimeMillis() - 24 * 3600 * 1000 * days;
        return dataReader.removeFromRedisSortedListByScore(genRecentDayListKeys(days, userId, userType), UserPointsLog.class, 0, time);

    }

    @Override
    public Future<List<UserPointsLog>> getRecentlyPointsType(long days, int limit, int offset, String userId, UserType userType) {

        long start = System.currentTimeMillis() - 24 * 3600 * 1000 * days;
        //  return dataReader.getAllDataInSortedListByScores(genRecentDayListKeys(days, userId, userType), start, Long.MAX_VALUE, UserPointsLog.class);
        return dataReader.getAllDataInSortedListWithScores(genRecentDayListKeys(days, userId, userType), 0-limit - offset,0-offset-1,  UserPointsLog.class);

    }

    private String genRecentDayListKeys(long days, UserPointsLog userPointsLog) {
        return genRecentDayListKeys(days, Long.toString(userPointsLog.getUserId()), userPointsLog.getUserType());
    }

    private String genRecentDayListKeys(long days, String userId, UserType userType) {
        return "Recent" + days + ":" + userId + ":" + userType;
    }

}
